home *** CD-ROM | disk | FTP | other *** search
- // Copyright: © 1993 Apple Computer, Inc. All rights reserved.
- // Author: John R. Powers, III
- // Date: 31-Jan-94
-
- /*
- This module is an example of an external module that can
- be called by Reno with a kResContext or kResEvent.
- See Reno ERS Part 4 - External Modules for more information.
-
- The structure being passed to the module is a simple array
- of bytes (a string) with no length byte or terminator.
-
- A kResEvent that would invoke this module would
- look like the following:
-
- // Beep Apple Event (External Module Test)
-
- resource kResEvent (1001, Purgeable) {
- front, // target
- 'ApCx', // class
- 'BEEP', // Apple event id
- none, // not used, always none.
- 'ctxt', // required key
- "1,2" // data - duration and count
- };
-
- A kResContext that would invoke this module requires
- that a "Beeper" case be added to the type
- definition for kResContext in RenoDataTypes.r. The
- following is an example:
-
- switch {
- …
- case Beeper:
- key longint = 'BEEP';
- string;
- …
-
- Then you can make as many kResContext's as you like
- in the Reno content file. They would look like the following:
-
- // Beep "Context Check" (External Module Test)
-
- resource kResContext (1001, Purgeable) {
- front, // target
- Beeper {
- "1,2" // data - duration and count
- },
- };
-
- In the kResContext example, the "1,2" string is NOT a
- pascal string. There should be no length byte. The
- resource contents should look something like this:
-
- f***BEEP1,2
-
- The target ('f***' for front application)
- and module name ('BEEP') are required and are followed by the
- data in string form. There is no length byte.
- A null terminator (IE C-string) is okay, but not required.
-
- History:
-
- 1.0d1e1 30-Jun-92 new
- 1.0d1e2 13-Jul-92 change file type from 'ctxt' to 'extm'
- change creator from 'ctxt' to 'reno'
- 1.0d2 24-Jul-92 parse string, remove header files.
- 1.0d3 23-Oct-92 tune-up for Reno 1.0a6.
- 1.0d4 18-Nov-92 make more data-fault-tolerant for Reno 1.0a7.
-
- 31-Jan-94 1.2a5e1 <3.01> Add "TextUtils.h" for build with ETO #13.
-
- There is NO Beep.h header file.
- There is NO Proto.h header file.
- There is NO Utility.h header file.
- There is NO AllHeaders.h header file.
- */
-
- // Toolbox headers
-
- #include "Types.h"
- #include "Memory.h"
- #include "OSUtils.h"
- #include "Packages.h"
- #include "TextUtils.h"
-
- // Prototypes
- OSErr
- setOutMessage(void* theData, Size theSize, void* outMessage, Size* outSize);
-
- void
- parseString (char* inStr, Size strLen, short pData[], short* dataCnt);
-
-
- // ------------------------------------------------------------------------
- // Entrypoint
- // We are passed a string containing the duration and count separated by commas.
- // The string has no length byte or terminator. Length is in inSize.
- //
- // msg is the incoming array of bytes.
- // inSize is the size of msg in bytes.
- // outMessage is an outgoing array of bytes.
- // outSize is the size of outMessage in bytes.
- // startGlobals is a handler to this module's startup globals, if any.
- //
- pascal OSErr
- main(char* msg, Size inSize, void* outMessage, Size* outSize, Handle /*startGlobals*/)
- {
- short i;
- long result=0;
- OSErr err = noErr;
- short dataCnt=0;
- short dataVal[10];
-
- if(inSize>0)
- {
- // We have incoming data,
- // parse string to get comma-separated values.
- parseString(msg, inSize, dataVal, &dataCnt);
- // If we have any data, use it.
- if (dataCnt>0)
- {
- short duration = dataVal[0];
- short count = dataVal[1];
- for(i=0; i<count; i++)
- {
- SysBeep(duration);
- }
- }
- }
- // Return the result.
- err = setOutMessage(&result, sizeof(Boolean), outMessage, outSize);
- return(err);
- }
-
- // ------------------------------------------------------------------------
- // parseString
- // Parse the string into a list of shorts.
- // Each item is separated by a comma.
- // Spaces and nulls ('\0') are ignored.
- // Two consecutive commas generate a zero value.
- // Caller MUST initialize dataCnt,
- // it is NOT initialized in this function, only incremented.
- // strLen is treated as a local variable initialized by the caller.
- void
- parseString (char* inStr, Size strLen, short dataVal[], short* dataCnt)
- {
- char ch;
- short itemLen=0;
- Str255 itemStr;
- long itemVal;
-
- while(strLen>0)
- {
- // Take character from string.
- ch = *inStr++;
- strLen--;
- // Is the character anything but a space, comma, or null?
- if(ch!=' ' && ch!=',' && ch!='\0')
- {
- // Add character to item string (skip over length byte).
- itemStr[++itemLen] = ch;
- }
- // Are we at the end of an item (comma) or the string?
- if(ch==',' || strLen==0)
- {
- // At the end of an item (a comma) or the string.
- // Convert string to value and add it to the array.
- // If string is empty, the value will be zero.
- itemStr[0] = itemLen;
- StringToNum(itemStr, &itemVal);
- dataVal[(*dataCnt)++] = (short) itemVal;
- itemLen = 0;
- }
- }
- }
-
- // ------------------------------------------------------------------------
- // setOutMessage
- // Create the outMessage array and transfer theData to it.
- // Set the outSize.
- // The receiver of the outMessage array must dispose the pointer.
- // This is done by Reno's Apple event handler for external modules.
- OSErr
- setOutMessage(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
- {
- Ptr p;
-
- if (p = NewPtr(theSize))
- {
- BlockMove(theData, p, theSize);
-
- *outSize = theSize;
- *outMessage = p;
-
- return(noErr);
- }
- else
- return(MemError());
- }
-
-